﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DengShijieWindowsFormsApp
{
    public partial class EmpolyeeRecordsForm: Form
    {
        private TreeNode tvRootNode;

        public EmpolyeeRecordsForm()
        {
            InitializeComponent();
            PopulateTreeView();
            initalizeListView();

        }

        private void EmpolyeeRecordsForm_Load(object sender, EventArgs e)
        {

        }
        private void PopulateTreeView()
        {
            statusBarPanel1.Tag = "Refreshing,Please wait...";
            this.Cursor = Cursors.WaitCursor;
            treeView1.Nodes.Clear();
            tvRootNode = new TreeNode("Emoplyee Records");
            this.Cursor = Cursors.Default;
            treeView1.Nodes.Add(tvRootNode);

            TreeNodeCollection nodeCollection = tvRootNode.Nodes;
            XmlTextReader reader = new XmlTextReader("C:\\Users\\Lenovo\\source\\repos\\cms\\DengShijieWindowsFormsApp\\DengShijieWindowsFormsApp\\EmpRec.xml");
            reader.MoveToElement();
            try {
                while (reader.Read()) {
                    if (reader.HasAttributes && reader.NodeType == XmlNodeType.Element) {
                        reader.MoveToElement();
                        reader.MoveToElement();

                        reader.MoveToAttribute("Id");
                        String strVal = reader.Value;

                        reader.Read();
                        reader.Read();
                        if (reader.Name == "Dept") {
                            reader.Read();
                        }
                        TreeNode EcodeNode = new TreeNode(strVal);
                        nodeCollection.Add(EcodeNode);
                    }
                }
                statusBarPanel1.Tag="click to see records";
            }
            catch (Exception ex){

                MessageBox.Show(ex.Message);
            }
        }

        protected void initalizeListView() {
            listView1.Clear();
            listView1.Columns.Add("Empolyee Name",255,HorizontalAlignment.Left);
            listView1.Columns.Add("Date of join", 70, HorizontalAlignment.Right);
            listView1.Columns.Add("Gread", 105, HorizontalAlignment.Left);
            listView1.Columns.Add("Salary", 105, HorizontalAlignment.Left);
        }

        protected void PopulateListView(TreeNode crrNode) {
            initalizeListView();
            XmlTextReader ListRead = new XmlTextReader("C:\\Users\\Lenovo\\source\\repos\\cms\\DengShijieWindowsFormsApp\\DengShijieWindowsFormsApp\\EmpRec.xml");
            ListRead.MoveToElement();
            while (ListRead.Read()) {
                String strNodeName;
                String strNodePath;
                String name;
                String gread;
                String doj;
                String sal;
                String[] strItemArr = new string[4];
                ListRead.MoveToFirstAttribute();
                strNodeName = ListRead.Value;
                strNodePath = crrNode.FullPath.Remove(0, 17);

                if (strNodePath == strNodeName) {
                    ListViewItem lvi;
                    ListRead.MoveToNextAttribute();
                    name = ListRead.Value;
                    lvi = listView1.Items.Add(name);
                    ListRead.Read();
                    ListRead.Read();

                    ListRead.MoveToFirstAttribute();
                    doj = ListRead.Value;
                    lvi.SubItems.Add(doj);

                    ListRead.MoveToNextAttribute();
                    gread = ListRead.Value;
                    lvi.SubItems.Add(gread);


                    ListRead.MoveToNextAttribute();
                    sal = ListRead.Value;
                    lvi.SubItems.Add(sal);

                    ListRead.MoveToNextAttribute();
                    ListRead.MoveToElement();
                    ListRead.ReadString();
                }
            
            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode currNode = e.Node;
            if (tvRootNode == currNode)
            {
                statusBarPanel1.Text = "double click";
                return;


            }
            else {
                statusBarPanel1.Text = "Click an Employee code to view record";
            }
            PopulateListView(currNode);
        }
    }
}
